﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;
using MSBTC.ReportViewer;
using MSBTC.WinFormHelpers;

namespace MSBTC.WinForms
{
    public partial class PMSForm : Form
    {
        MDIParent MForm = new MDIParent();
        PMSContext dbcontext = new PMSContext();
        Project dbpj = new Project();
        FormHelpers help = new FormHelpers();

        public PMSForm()
        {
            InitializeComponent();
        }

        private void PMSForm_Load(object sender, EventArgs e)
        {
            cmbProj.SelectedIndex = 0;
            LLPrint.Enabled = false;
            var proj = (from p in dbcontext.Projects
                        select p).ToList();
            if (proj != null)
            {
                foreach (var prj in proj)
                {
                    cmbProj.Items.Add(prj.Project_Name);
                }

            }
        }

        private void PMSForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dlg = MessageBox.Show("You want to close Form?", "Warning", MessageBoxButtons.YesNo);
            if (dlg == DialogResult.No) 
                e.Cancel = true;
            else if (dlg == DialogResult.No)
                e.Cancel = false;
        }

        #region LinkLabelClicked
        private void LLCost_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            help.getPname(lblPname.Text);
            help.GetCost(lblCost.Text);
            help.GetBudget(lblbud.Text);
            help.GetRemain(lblrem.Text);

            CostRptForm CForm = new CostRptForm();

            CForm.Show();

        }

        private void LLPrint_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            help.getPname(lblPname.Text);

            PMSRptForm rpt = new PMSRptForm();

            rpt.Show();
        }
        #endregion

        #region LinkLabelHover
        
        private void LLCost_MouseHover(object sender, EventArgs e)
        {
            LLCost.Cursor = Cursors.Hand;
        }

        private void LLPrint_MouseHover(object sender, EventArgs e)
        {
            LLPrint.Cursor = Cursors.Hand;
        }
        #endregion

        private void cmbProj_SelectedIndexChanged(object sender, EventArgs e)
        {
            int totcost = 0;

            if (cmbProj.SelectedIndex == 0)
            {
                LLPrint.Enabled = false;
                LVMaterials.Items.Clear();
                LVEquipment.Items.Clear();
                lvLab.Items.Clear();
                lblPname.Text = "";
                lblCname.Text = "";
                lblstat.Text = "";
                lblCost.Text = "";
                lblbud.Text = "";
                lblrem.Text = "";
            }
            else
            {
                LVMaterials.Items.Clear();
                LVEquipment.Items.Clear();
                lvLab.Items.Clear();
                LLPrint.Enabled = true;

                var proj = (from p in dbcontext.Projects
                            where p.Project_Name == cmbProj.SelectedItem
                            select p).SingleOrDefault();
                if (proj != null)
                {
                    lblPname.Text = proj.Project_Name;
                    lblstat.Text = proj.Status;
                    lblbud.Text = proj.Budget.ToString("N");

                    var cname = (from c in dbcontext.Customers
                                 where c.Customerid_PK == proj.Customerid_FK
                                 select c).SingleOrDefault();
                    if (cname != null)
                    {
                        lblCname.Text = cname.Customer_Name;
                    }

                    lblstat.Text = proj.Status;
                    if (lblstat.Text == "Active")
                    {
                        lblstat.ForeColor = Color.Green;
                    }
                    else
                    {
                        lblstat.ForeColor = Color.Red;
                    }

                    lblbud.Text = proj.Budget.ToString("N");

                    #region ListviewItems
                    //Equipments
                    var pjeq = (from eq in dbcontext.PrjEqs
                                where eq.Projectid_FK == proj.Projectid_PK
                                select eq).ToList();
                    if (pjeq != null)
                    {
                        foreach (var peq in pjeq)
                        {
                            var eqmt = (from em in dbcontext.Equipments
                                        where em.Equipmentid_PK == peq.Equipid_FK
                                        select em).ToList();
                            if (eqmt != null)
                            {
                                foreach (var eqp in eqmt)
                                {
                                    ListViewItem lvitems = LVEquipment.Items.Add(eqp.Equipmentid_PK.ToString());
                                    lvitems.SubItems.Add(eqp.Equip_Name.ToString());
                                    lvitems.SubItems.Add(eqp.Description.ToString());
                                    lvitems.SubItems.Add(eqp.Quantity.ToString());
                                    var preq = (from p in dbcontext.PrjEqs
                                                where p.Equipid_FK == eqp.Equipmentid_PK
                                                && p.Projectid_FK == proj.Projectid_PK
                                                select p).SingleOrDefault();
                                    if (preq != null)
                                    {
                                        lvitems.SubItems.Add(preq.QuantityUsed.ToString());
                                    }
                                    else
                                    {
                                        lvitems.SubItems.Add("0");
                                    }
                                }
                            }
                        }
                        this.Refresh();
                    }

                    //materials
                    int matcost = 0;
                    var pjmat = (from eq in dbcontext.PrjMatls
                                 where eq.Projectid_FK == proj.Projectid_PK
                                 select eq).ToList();
                    if (pjmat != null)
                    {
                        foreach (var pm in pjmat)
                        {
                            var mat = (from em in dbcontext.MaterialsInventorys
                                       where em.MaterialsInventoryid_PK == pm.MaterialsInventoryid_FK
                                       select em).ToList();
                            if (mat != null)
                            {

                                foreach (var pmt in mat)
                                {
                                    ListViewItem lvitems = LVMaterials.Items.Add(pmt.MaterialsInventoryid_PK.ToString());
                                    lvitems.SubItems.Add(pmt.Material_Name.ToString());
                                    lvitems.SubItems.Add(pmt.Qty.ToString());
                                    lvitems.SubItems.Add(pmt.Unit.ToString());
                                    lvitems.SubItems.Add(pmt.Brand_Name.ToString());

                                    var pjm = (from p in dbcontext.PrjMatls
                                               where p.MaterialsInventoryid_FK == pmt.MaterialsInventoryid_PK
                                               && p.Projectid_FK == proj.Projectid_PK
                                               select p).SingleOrDefault();
                                    if (pjm != null)
                                    {                                     
                                        lvitems.SubItems.Add(pjm.QtyUsed.ToString());
                                    }
                                    else
                                    {
                                        lvitems.SubItems.Add("0");
                                    }

                                    //cost for each materials
                                    int prod = pjm.QtyUsed * pmt.UnitCost;
                                    matcost += prod;
                                    lvitems.SubItems.Add(pmt.UnitCost.ToString());
                                    int crit = pmt.Qty - pmt.QtyUsed;
                                    if (crit <= 10)
                                    {
                                        MessageBox.Show("The Material " + pmt.Material_Name + " is at critical point of " + crit + " remaining quantity, Please order new sets of " + pmt.Material_Name + " in Purchase Order", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                    }
                                }
                            }
                        }
                        this.Refresh();

                        //laborer
                        int atotcost = 0;
                        var labr = (from l in dbcontext.Labors
                                    where l.Projectid_FK == proj.Projectid_PK
                                    select l).ToList();
                        if (labr != null)
                        {
                            foreach (var lab in labr)
                            {
                                ListViewItem lvitems = lvLab.Items.Add(lab.Labourid_PK.ToString());
                                lvitems.SubItems.Add(lab.Labour_Name.ToString());
                                lvitems.SubItems.Add(lab.Position.ToString());
                                lvitems.SubItems.Add(lab.Rate.ToString());
                                lvitems.SubItems.Add(lab.Net_Paid.ToString());
                                lvitems.SubItems.Add(lab.Total.ToString());

                                atotcost += Convert.ToInt32(lab.Total);
                            }
                        }
                    #endregion
                        int costing = 0;
                        var est = (from m in dbcontext.Materials
                                   where m.Projectid_FK == proj.Projectid_PK
                                   select m).ToList();
                        if (est.Count != 0)
                        {
                            foreach (var costs in est)
                            {
                                costing += Convert.ToInt32(costs.Tot_Cost);
                            }
                        }

                        
                        lblCost.Text = (matcost + atotcost).ToString("N");
                        int ans = matcost + atotcost;
                        lblrem.Text = (proj.Budget - ans).ToString("N");
                    }
                }
            }
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {

            DialogResult YN = MessageBox.Show("You are going to end an on-going Project and cannot be able to undo changes, Do you still want to proceed?", "Project Monitoring", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (YN == DialogResult.Yes)
            {

                var cust = (from c in dbcontext.Customers
                            where c.Customer_Name == lblCname.Text
                            select c).SingleOrDefault();

                var proj = (from p in dbcontext.Projects
                            where p.Project_Name == cmbProj.SelectedItem &&
                            p.Customerid_FK == cust.Customerid_PK
                            select p).SingleOrDefault();
                if (proj != null)
                {

                    proj.Status = "Finished";
                    dbcontext.Entry(proj).State = EntityState.Modified;
                    dbcontext.SaveChanges();

                    MessageBox.Show("Successfully updated a Project.", "Project Monitoring", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    lblstat.Refresh();
                    return;
                }
            }
        }

       
    }

}
